home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / jcool01.zip / QUEUE.H < prev    next >
C/C++ Source or Header  |  1992-09-29  |  8KB  |  192 lines

  1. //
  2. // Copyright (C) 1991 Texas Instruments Incorporated.
  3. //
  4. // Permission is granted to any individual or institution to use, copy, modify,
  5. // and distribute this software, provided that this complete copyright and
  6. // permission notice is maintained, intact, in all copies and supporting
  7. // documentation.
  8. //
  9. // Texas Instruments Incorporated provides this software "as is" without
  10. // express or implied warranty.
  11. //
  12. // Created: MJF 05/22/89 -- Initial design.
  13. // Updated: JCB 06/05/89 -- Implementation.
  14. // Updated: LGO 08/09/89 -- Inherit from Generic
  15. // Updated: MBN 08/20/89 -- Changed usage of template to reflect new syntax
  16. // Updated: MBN 08/24/89 -- Added conditional exception handling and base class
  17. // Updated: MBN 10/07/89 -- Fixed element count bug in get() method
  18. // Updated: MBN 10/11/89 -- Changed "current_position" to "curpos" 
  19. // Updated: MBN 10/19/89 -- Added optional argument to set_compare method
  20. // Updated: MBN 11/01/89 -- Added constructor with user-provided storage param
  21. // Updated: MBN 11/15/89 -- Fixed put() for case when there is no storage
  22. // Updated: MBN 11/17/89 -- Fixed unget() for case when there is no storage
  23. // Updated: LGO 02/02/90 -- Re-wrote practically everything
  24. // Updated: MBN 02/22/90 -- Changed size arguments from long to unsigned long
  25. // Updated: MJF 03/12/90 -- Added group names to RAISE
  26. // Updated: MJF 05/31/90 -- Use "delete [size] data"
  27. // Updated: MJF 06/30/90 -- Added base class name to constructor initializer
  28. // Updated: VDN 02/21/92 -- New lite version
  29. // Updated: JAM 08/19/92 -- removed DOS specifics, stdized #includes
  30. // Updated: JAM 08/19/92 -- modernized template syntax, remove macro hacks
  31. //                          non-template classes Cool_Stack=>CoolBase_Stack
  32. // Updated: JAM 09/29/92 -- index/size/pos type changed from [u]long to [u]int
  33. //
  34. // The Queue<Type> class is  publicly  derived from   the base Queue class  and
  35. // implements a circular buffer of a user-specified type.  This is accomplished
  36. // by  using the  parameterized type capability   of C++. The Queue  will  grow
  37. // dynamically as necessary with the amount of growth  determined by  the value
  38. // of an allocation   size slot. Fixed length queues    are also supported   by
  39. // setting the value of the allocation size slot to INVALID.
  40. //
  41. // The (Base) Queue object contains in, out and limit indices into data
  42. // which is in the Queue<Type> object.
  43. // If in == out, the buffer is empty.
  44. // If in > out, the area from out to in-1 contains available data.
  45. // If out > in, the area from out to limit-1 contains the first part of
  46. // the available data, and the area from first to in-1 contains the rest.
  47. // Note that if the buffer was completely full, in would equal out. We
  48. // we never let this happen, to preserve a simple empty check.
  49. //
  50. // There  are four   constructors   for the   Queue<Type> class.   The    first
  51. // constructor takes  no arguments and creates   an  empty  Queue object of the
  52. // specified type.  The second constructor takes a required argument specifying
  53. // the initial  size  of the queue.  The  third takes a pointer   to a block of
  54. // user-defined storage    and  the number of  elements  the   queue  can hold.
  55. // Finally,  the   third constructor  takes a  single argument  consisting of a
  56. // reference to a Queue<Type> and duplicates its size and element values.
  57. //
  58. // Methods are  provided to get  and unget an item  from the queue, and put and
  59. // unput and item from the queue. This provides addition and removal operations
  60. // for both ends of the queue. A look  method allows an  application to examine
  61. // the item at the head of the queue without removing it. Methods to report the
  62. // number of items in the queue, check the empty  status,  and clear  all items
  63. // from the  queue  are also provided.  The   assignment, output, and  equality
  64. // operators are overloaded and two methods to  set  the allocation growth size
  65. // and compare function are available. Finally, find and  remove  methods allow
  66. // an application  to search for  a particular item and/or  remove   it  from a
  67. // queue.
  68. //
  69.  
  70. #ifndef QUEUEH                    // If no Queue class definition
  71. #define QUEUEH                    // Indiciate we have done it
  72.  
  73. #ifndef BASE_QUEUEH                // If no definition for class
  74. #include <cool/Base_Queue.h>                // include definition file
  75. #endif
  76.  
  77. template <class Type>
  78. class CoolQueue : public CoolBase_Queue {
  79. public:
  80.   typedef Boolean (*Compare) (const Type&, const Type&);
  81.   CoolQueue();                // CoolQueue q;
  82.   CoolQueue(unsigned int);        // CoolQueue q(10);
  83.   CoolQueue(void*, unsigned int);    // CoolQueue with static storage
  84.   CoolQueue(const CoolQueue<Type>&);    // CoolQueue q = q2;
  85.   ~CoolQueue();                // CoolQueue destructor
  86.  
  87.   Type& get ();                    // Get and remove first-in item
  88.   Boolean get (Type& result);            // Get and remove first-in item
  89.   Boolean unget (const Type&);            // Put back first-in item 
  90.   Boolean put (const Type&);            // Put new last-in item
  91.   Type& unput();                // Remove last-in item 
  92.   Boolean unput (Type& result);            // Remove last-in item 
  93.   inline Type& look();                // Just return next item to get
  94.  
  95.   inline Type& value ();            // Get current position value
  96.   Boolean find (const Type&);            // Find/set current position 
  97.   Boolean remove ();                // Remove current position item
  98.   Boolean remove (const Type&);            // Find/remove item 
  99.   
  100.   CoolQueue<Type>& operator= (const CoolQueue<Type>&);    // Assignment q = q2;
  101.   Boolean operator== (const CoolQueue<Type>&) const; // is equal
  102.   inline Boolean operator!= (const CoolQueue<Type>&) const; // is not equal
  103.  
  104.   void resize (int);                // Resize for at least count
  105.   inline void set_growth_ratio (float);        // Set growth percentage
  106.   inline void set_alloc_size (int);        // Set alloc size
  107.   void set_compare(Compare = NULL); // Set compare function
  108.  
  109.   friend ostream& operator<< (ostream&, const CoolQueue<Type>&); 
  110.   /*inline##*/ friend ostream& operator<< (ostream&, const CoolQueue<Type>*);
  111.  
  112. protected:
  113.   Type* data;                    // Pointer to allocated storage
  114.   static Compare compare_s;    // Pointer operator== function
  115.   Boolean grow ();                // Make the queue bigger
  116.   ostream& qprint(ostream& os) const;        // Print a queue
  117.  
  118. private:
  119.   friend Boolean CoolQueue_is_data_equal (const Type&, const Type&);
  120. };
  121.  
  122.  
  123. // Type& look () -- Return first-in item in this CoolQueue
  124. // Input:           None
  125. // Output:          Reference to the first-in item 
  126.  
  127. template<class Type> 
  128. inline Type& CoolQueue<Type>::look () {
  129. #if ERROR_CHECKING
  130.   if (in == out)                // If there are no elements
  131.     this->look_error (#Type);            // Raise exception
  132. #endif
  133.   return (this->data[out]);
  134. }
  135.  
  136.  
  137. // Type& value () -- Return item at current position
  138. // Input:            None
  139. // Output:           Reference to item at current position 
  140.  
  141. template<class Type> 
  142. inline Type& CoolQueue<Type>::value () {
  143. #if ERROR_CHECKING
  144.   if (this->curpos == in)            // If invalid current position
  145.     this->value_error (#Type);            // Raise exception
  146. #endif
  147.   return (this->data[curpos]);
  148. }
  149.  
  150.  
  151. // Boolean operator!= (CoolQueue<Type>&) -- Compare this CoolQueue with another CoolQueue
  152. // Input:  Reference to a CoolQueue
  153. // Output: TRUE or FALSE
  154.  
  155. template<class Type> 
  156. inline Boolean CoolQueue<Type>::operator!= (const CoolQueue<Type>& q) const {
  157.   return (!operator==(q));
  158. }
  159.  
  160.  
  161. // void set_growth_ratio(float) -- Set growth percentage of this CoolQueue
  162. // Input:                          Float ratio
  163. // Output:                         None
  164.  
  165. template<class Type> 
  166. inline void CoolQueue<Type>::set_growth_ratio (float ratio) {
  167.   this->CoolBase_Queue::set_growth_ratio (ratio, "Type");    // Pass size/type to base class
  168. }
  169.  
  170.  
  171. // void set_alloc_size(int) -- Set the default allocation size growth size
  172. // Input:  Integer size
  173. // Output: None
  174.  
  175. template<class Type> 
  176. inline void CoolQueue<Type>::set_alloc_size (int size) {
  177.   this->CoolBase_Queue::set_alloc_size (size, "Type");    // Pass size/type to base
  178. }
  179.  
  180. // operator<< -- Overload the output operator for CoolQueue
  181. // Input:        ostream reference, queue pointer
  182. // Output:       CoolQueue data is output to ostreamn
  183.  
  184. template<class Type>
  185. inline ostream& operator<< (ostream& os, const CoolQueue<Type>* q) {
  186.   return q->qprint(os);
  187. }
  188.  
  189.  
  190. #endif                // End #ifdef of QUEUEH
  191.  
  192.